import sys
import os.path


def read_in_chunks(file_object, chunk_size=1024):
    """Lazy function (generator) to read a file piece by piece.
    Default chunk size: 1k."""
    while True:
        data = file_object.read(chunk_size)
        if not data:
            break
        yield data


def removeGershaim():
    content = ""
    the_file = open("workspace\\mythesis\\Thesis\\src\\NewxmlStructureFormatFile.txt",'w',encoding='utf-8')
    content_file = open("workspace\\mythesis\\Thesis\\src\\apiCalls.txt","r", encoding='utf-8')
    
    piece = content_file.read()
    content = piece.replace("\"","")
    the_file.write(content)
    
    #for piece in content_file:  
    #    content = piece.replace("\"", "")
    #    the_file.write(content)
    #    the_file.write('\n')
    #    content =""
         
    the_file.close()
    content_file.close()
  
  #appCallRecord_id,  processId,function,timeoffset,proccessName
def csvToTextFileByProccessId(file_object):
    file = file_object.readlines()
    file = file[1:]
    prevProcessId = 0
    currProcessId = 0
    writeToFile =open("0.txt",'w')
    bool= True
    for line in file:        
        arr = line.split(',')
        print (len(arr))
        print(arr)
        currProcessId = arr[1]
        if  not(currProcessId == prevProcessId) :
            writeToFile.close()
            writeToFile = open( ""+str(prevProcessId)+".txt",'w')
            prevProcessId = currProcessId 
            
        print (arr[2][13:])
        writeToFile.write(" "+arr[2][13:])
        
    writeToFile.close()



def writeTextXMLFormatToFile(txt,filenumber):

    filepath =  "C:\\Users\\Polak\\Desktop\\Documents\\University\\thesis\\updateDataBase\\processData\\"+(str(filenumber))+".txt"
    txt = txt.replace("\"","")
    file = open(filepath, 'w')
    file.write(txt)
    file.close()
        
    
def processCSVfileFromTableToXML(folder):
    filenumber = 1
    for filename in os.listdir (folder): #iterate files in directory
        with open(""+folder+"\\"+filename) as current_file:
            line = current_file.readline() # first line.
            currTextToSave = ""
            newTextToSave  = ""
            boolend = False
            first = True
            for line in current_file:
                if len(line) <=1:
                    continue
                if(line[0] == '\"'):
                    boolend =  True
                    csv = line.split(",")
                    num = csv[len(csv)-1]
                    newTextToSave += num #append last coulmn in csv file
                else:
                    currTextToSave += line
                    
                if boolend : 
                    boolend = False
                   
                    if first:
                        first = False
                        currTextToSave = newTextToSave
                        newTextToSave = ""
                        continue
                    writeTextXMLFormatToFile(currTextToSave,filenumber) # write xml to file inorder to read it after.
                    currTextToSave = newTextToSave
                    newTextToSave = ""
                    filenumber +=1
            
        
        
if __name__ == '__main__':
    filenumber = 0
    folderpath = "C:\\Users\\Polak\\Desktop\\Documents\\University\\thesis\\updateDataBase\\july"
    processCSVfileFromTableToXML(folderpath)
    
     #the_file = open("dataset.csv",'r')
     #csvToTextFileByProccessId(the_file)
     #the_file.close()
     
     
     
     
     
     